import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Home from '../views/home.vue'
import BlindBoxDetail from '../views/blindBoxDetail.vue'
import FillAddress from '../views/fillAddress.vue'
import MyAddress from '../views/myAddress.vue'
import EdmitAddress from '../views/edmitAddress.vue'
import PostDone from '../views/postDone.vue'
import MyBlindBox from '../views/myBlindBox.vue'
import Rule from '../views/rule.vue'
import ContactWays from '../views/contactWays.vue'
import CashCoupon from '../views/cashCoupon.vue'
import Exchange from '../views/exchange.vue'
import ExchangeCenter from '../views/exchangeCenter.vue'
import VersionUpgrade from '../views/versionUpgrade.vue'
import Mine from '../views/mine.vue'
import LeaveMessageList from '../views/leaveMessageList.vue'
import LeaveMessage from '../views/leaveMessage.vue'
import LeaveMessageDetail from '../views/leaveMessageDetail.vue'
import BoxCashCoupon from '../views/boxCashCoupon.vue'
import PayInfo from '../views/payInfo.vue'
import ChargeTelephone from '../views/chargeTelephone.vue'
import RechargeStatus from '../views/rechargeStatus.vue'

import store from '../store'
import utilTool from '@/common/utils/utils.js'
import apiUser from '@/common/api/apiUser.js'

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    meta: { title: '盲盒抽奖' },
    component: Home,
  },
  {
    path: '/blindBoxDetail',
    name: 'BlindBoxDetail',
    meta: { title: '盲盒详情' },
    component: BlindBoxDetail,
  },
  {
    path: '/fillAddress',
    name: 'FillAddress',
    meta: { title: '确认邮寄信息' },
    component: FillAddress,
  },
  {
    path: '/myAddress',
    name: 'MyAddress',
    meta: { title: '我的地址' },
    component: MyAddress,
  },
  {
    path: '/edmitAddress',
    name: 'EdmitAddress',
    meta: { title: '编辑地址' },
    component: EdmitAddress,
  },
  {
    path: '/postDone',
    name: 'PostDone',
    meta: { title: '确认邮寄信息' },
    component: PostDone,
  },
  {
    path: '/myBlindBox',
    name: 'MyBlindBox',
    meta: { title: '我的盲盒' },
    component: MyBlindBox,
  },
  {
    path: '/rule',
    name: 'Rule',
    meta: { title: '出盒规则' },
    component: Rule,
  },
  {
    path: '/contactWays',
    name: 'ContactWays',
    meta: { title: '客服' },
    component: ContactWays,
  },
  {
    path: '/cashCoupon',
    name: 'CashCoupon',
    meta: { title: '现金券' },
    component: CashCoupon,
  },
  {
    path: '/exchange',
    name: 'Exchange',
    meta: { title: '兑换中心' },
    component: Exchange,
  },
  {
    path: '/exchangeCenter',
    name: 'ExchangeCenter',
    meta: { title: '兑换中心' },
    component: ExchangeCenter,
  },
  {
    path: '/versionUpgrade',
    name: 'VersionUpgrade',
    meta: { title: '' },
    component: VersionUpgrade,
  },
  {
    path: '/mine',
    name: 'Mine',
    meta: { title: '个人中心' },
    component: Mine,
  },
  {
    path: '/leaveMessageList',
    name: 'LeaveMessageList',
    meta: { title: '留言反馈' },
    component: LeaveMessageList,
  },
  {
    path: '/leaveMessage',
    name: 'LeaveMessage',
    meta: { title: '留言' },
    component: LeaveMessage,
  },
  {
    path: '/leaveMessageDetail',
    name: 'LeaveMessageDetail',
    meta: { title: '留言详情' },
    component: LeaveMessageDetail,
  },
  {
    path: '/boxCashCoupon',
    name: 'BoxCashCoupon',
    meta: { title: '抽盒券' },
    component: BoxCashCoupon,
  },
  {
    path: '/payInfo',
    name: 'PayInfo',
    meta: { title: '确认信息' },
    component: PayInfo,
  },
  {
    path: '/chargeTelephone',
    name: 'ChargeTelephone',
    meta: { title: '充话费' },
    component: ChargeTelephone,
  },
  {
    path: '/rechargeStatus',
    name: 'RechargeStatus',
    meta: { title: '充值状态' },
    component: RechargeStatus,
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

const appId = process.env.VUE_APP_APPID
const scope = process.env.VUE_APP_SCOPE
// 通过code获取用户Id
function getUserIdByCode(code, succCb) {
  apiUser
    .userLoginByCode({
      code: code,
    })
    .then((data) => {
      store.dispatch('user/setUserInfo', data.data)
      succCb()
    })
}

router.beforeEach((to, from, next) => {
  let code = utilTool.getUrlParam('code')
  let userInfo = store.getters.userInfo
  if (userInfo && userInfo.userId) {
    console.log('userinfo', '111111111')
    return next()
  }
  if (code) {
    console.log('code', '22222222')
    getUserIdByCode(code, () => {
      next()
    })
  } else {
    console.log('jump', '3333333')
    let callBackUrl = encodeURIComponent(window.location.href)
    let authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${callBackUrl}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`
    window.location.replace(authUrl)
  }
})
export default router
